package com.springboot.redis;

import lombok.Data;

import java.io.Serializable;

/**
 * @author chenxubing
 */
@Data
public class MessageBody implements Serializable {
    private static final long serialVersionUID = 1L;
    private int code;
    private String msg;
    private Object extInfo;
    private Object data;
    private boolean success;


    public static MessageBody getMessageBody(boolean success, Object data) {
        MessageBody body = new MessageBody();
        setVal(success,body);
        body.setData(data);
        return body;
    }

    public static MessageBody getMsgSuccc() {
        MessageBody body = new MessageBody();
        setVal(true,body);
        return body;
    }

    public static MessageBody getMsgFail() {
        MessageBody body = new MessageBody();
        setVal(false,body);
        return body;
    }

    public static MessageBody getErrorMessageBody(String errorInfo) {
        MessageBody body = new MessageBody();
        setVal(false,body);
        body.setMsg(errorInfo);
        return body;
    }

    public static MessageBody getErrorMessageBody(String errorInfo, int errorCode) {
        MessageBody body = new MessageBody();
        body.setSuccess(false);
        body.setMsg(errorInfo);
        body.setCode(errorCode);
        return body;
    }


    public static MessageBody getMessageBody(Object data) {
        MessageBody body = new MessageBody();
        setVal(true,body);
        body.setData(data);
        return body;
    }

    public static MessageBody getInsideError() {
        MessageBody body = new MessageBody();
        setVal(false,body);
        return body;
    }

    private static void setVal(boolean success,MessageBody body){
        body.setSuccess(success);
        if(success){
            body.setCode(EnumError.SUCCESS.getCode());
            body.setMsg(EnumError.SUCCESS.getMsg());
        }else{
            body.setCode(EnumError.FAILED.getCode());
            body.setMsg(EnumError.FAILED.getMsg());
        }
    }
}